﻿using RightControl.IRepository;
using RightControl.IService;
using RightControl.Model;
using RightControl.Repository;
using System.Collections.Generic;

namespace RightControl.Service {
    public class PlatformAuthService : BaseService<PlatformAuth>, IPlatformAuthService {
        public IPlatformAuthRepository repository { get; set; }

        public PlatformAuth GetAuthByUserId(PlatformAuth filter, PageInfo pageInfo) {         
            dynamic dRet = GetListByFilter(filter, pageInfo);
            if (dRet!=null && dRet.count >0) {
                List<PlatformAuth> ret = dRet.data as List<PlatformAuth>;
                if (ret != null && ret.Count > 0) {
                    return ret[0];
                }
            }
            return new PlatformAuth();          
        }        

        public dynamic GetListByFilter(PlatformAuth filter, PageInfo pageInfo) {
            string _where = " where 1=1";
            _where += string.Format(" and UserId = {0} and Status=1", filter.UserId);
            if (filter.PlatForm > 0) {
                _where += string.Format(" and PlatForm = {0}", filter.PlatForm);
            }
            return GetListByFilter(filter, pageInfo, _where);
        }

        public bool IsNotExist(PlatformAuth filter) {
            string _where = " where 1=1";
            _where += string.Format(" and UserId = {0} and Status=1", filter.UserId);
            if (filter.PlatForm > 0) {
                _where += string.Format(" and PlatForm = {0}", filter.PlatForm);
            }

            return GetTotal(new SearchFilter() { pageIndex = 1, pageSize = 100, where = _where }) == 0;
        }

        public bool Delete(PlatformAuth filter) {            
            string _where = " where 1=1";
            _where += string.Format(" and Id = {0} ", filter.Id);
            _where += string.Format(" and UserId = {0} and Status=1", filter.UserId);
            _where += string.Format(" and PlatForm = {0} ", filter.PlatForm);
            return DeleteByWhere(_where);
        }
    }
}